﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Help;
using IBLL;
using Model;

namespace BLLA
{
    public partial class Order_DishesType_T : BaseBLL<Model.Order_DishesType_T>, IBLL.IOrder_DishesType_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();

        /// <summary>
        /// 更新一级商品分类信息
        /// </summary>
        /// <param name="OperID">操作员ID</param>
        /// <param name="json">更新信息</param>
        /// <param name="tag">操作标签（1：新增；2：修改；3：删除）</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string SetOrder_DishesType(int OperID, string json, int tag, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                Model.Order_DishesType_T info = ParseClass<Model.Order_DishesType_T>.StrDeserialize(json, RETID);
                string T = "";
                int ret = -1;
                int pos = 0;
                List<Model.Order_DishesType_T> ls = new List<Model.Order_DishesType_T>();
                ls = base.GetListBy(u => u.Stat == 1 && (u.PantID == "" || u.PantID == null)).ToList();
                if (ls.Count() >= 0)
                {
                    pos = ls.Count() + pos;
                }
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1:
                        T = "新增";
                        info.DishesTypeID = GetSysKey.GetKey("DishesTypeKey");
                        info.Stat = 1;
                        info.Postion = pos;
                        ret = base.Add(info);
                        break;
                    case 2:
                        T = "修改";
                        Model.Order_DishesType_T tmp = base.GetListBy(u => u.DishesTypeID == info.DishesTypeID).First();
                        Help.FuZhi<Model.Order_DishesType_T>.FZ(ref tmp, info);
                        tmp.Stat = 1;
                        tmp.Postion = info.Postion;
                        ret = base.Modify(tmp);
                        break;
                    case 3:
                        T = "删除";
                        if (base.DBSession.IOrder_Dishes_TDAL.GetListBy(u => u.DishesTypeID == info.DishesTypeID & u.DishesTypeState == 1).Count > 0)
                        {
                            result.BS = "-2";
                            result.Msg = "还有菜品不允许删除";
                            return os.StringOfObject(result, RETID);
                        }
                        tmp = base.GetListBy(u => u.DishesTypeID == info.DishesTypeID).First();
                        ret = base.Del(tmp);
                        break;
                }

                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "更新商品分类信息";
                    Log.OperationText = T + "商品分类信息成功";
                    Log.OperationType = "1F0006";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商品分类信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 更新二级商品分类信息
        /// </summary>
        /// <param name="OperID"></param>
        /// <param name="json"></param>
        /// <param name="tag"></param>
        /// <param name="PantID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string SetOrder_DishesType2(int OperID, string json, int tag, string PantID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                Model.Order_DishesType_T info = ParseClass<Model.Order_DishesType_T>.StrDeserialize(json, RETID);
                string T = "";
                int ret = -1;
                int pos = 1;
                List<Model.Order_DishesType_T> ls = new List<Model.Order_DishesType_T>();
                ls = base.GetListBy(u => u.Stat == 1 && u.PantID == PantID).ToList();
                if (ls.Count() >= 0)
                {
                    pos = ls.Count() + pos;
                }
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1:
                        T = "新增";
                        info.DishesTypeID = GetSysKey.GetKey("DishesTypeKey");
                        info.Stat = 1;
                        info.PantID = PantID;
                        info.Postion = pos;
                        ret = base.Add(info);
                        break;
                    case 2:
                        T = "修改";
                        Model.Order_DishesType_T tmp = base.GetListBy(u => u.DishesTypeID == info.DishesTypeID).First();
                        Help.FuZhi<Model.Order_DishesType_T>.FZ(ref tmp, info);
                        tmp.Stat = 1;
                        tmp.Postion = info.Postion;
                        ret = base.Modify(tmp);
                        break;
                    case 3:
                        T = "删除";
                        if (base.DBSession.IOrder_Dishes_TDAL.GetListBy(u => u.DishesTypeID == info.DishesTypeID & u.DishesTypeState == 1).Count > 0)
                        {
                            result.BS = "-2";
                            result.Msg = "还有菜品不允许删除";
                            return os.StringOfObject(result, RETID);
                        }
                        tmp = base.GetListBy(u => u.DishesTypeID == info.DishesTypeID).First();
                        ret = base.Del(tmp);
                        break;
                }

                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "更新商品分类信息";
                    Log.OperationText = T + "商品分类信息成功";
                    Log.OperationType = "1F0006";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商品分类信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);

        }

        /// <summary>
        /// 获取商品二级分类信息
        /// </summary>
        /// <param name="MerID">商户ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式（1：JSON；2：XML）</param>
        /// <returns></returns>
        public string Get_DishesType(string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion 
                List<Model.Order_DishesType_T> ls = new List<Model.Order_DishesType_T>();
                ls = base.GetListBy(u => u.Stat == 1 && (u.PantID != "" || u.PantID != null));
                //if (depid == "")
                //{
                //    ls = base.GetListBy(u => u.Stat == 1 & u.DishesTypeID.Length == 3);
                //}
                //else
                //{
                //    ls = base.GetListBy(u => u.Stat == 1 & u.DishesTypeID.StartsWith(depid) & u.DishesTypeID.Length == 6);
                //}
                if (ls != null & ls.Count > 0)
                {
                    object tem = from m in ls select new { m.DishesTypeID, m.DishesTypeName };

                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = tem;
                    result.total = ls.Count;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品分类信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        #region 获取商品一二级分类全部信息                
        /// <summary>
        /// 获取商品一二级分类信息
        /// </summary>
        /// <returns></returns>
        public string Get_Order_DishesType()
        {
            try
            {
                List<Model.Order_DishesType_T> dstList = base.GetListBy(u => true);
                List<Model.Order_DishesType_T> rootDishesType = dstList.Where(p => p.Stat == 1 && (p.PantID == null || p.PantID == "")).OrderBy(u => u.Postion).ToList();//找根节点 
                List<DishesTypeInfo> order_dishestype = new List<DishesTypeInfo>();
                if (rootDishesType == null)
                {
                    result.BS = "0";
                    result.Msg = "没有找到根节点";
                    return os.StringOfObject(result, 1);
                }
                //构建树形结构
                foreach (Model.Order_DishesType_T item in rootDishesType)
                {
                    DishesTypeInfo odt = new DishesTypeInfo
                    {
                        DishesTypeID = item.DishesTypeID,
                        DishesTypeName = item.DishesTypeName,
                        IntBZ = item.IntBZ,
                        Postion = item.Postion,
                        Stat = item.Stat,
                        Icon = item.Icon,
                        PantID = item.PantID,
                        Children = new List<DishesTypeInfo>()
                    };
                    AddSonNode(odt.DishesTypeID, odt, dstList);
                    result.total = odt.Children.Count + 1;
                    result.rows = odt;

                    order_dishestype.Add(odt);
                }

                return os.StringOfObject(order_dishestype, 1);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品二级级分类名称失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
            }
            return "";
        }

        /// <summary>
        /// 添加子集
        /// </summary>
        /// <param name="p">父ID</param>
        /// <param name="odt">父节点</param>
        /// <param name="roomDepList">数据集合</param>
        private void AddSonNode(string p, DishesTypeInfo odt, List<Model.Order_DishesType_T> roomDisList)
        {
            List<Model.Order_DishesType_T> dis = roomDisList.Where(x => x.PantID == p && x.Stat == 1).OrderBy(u => u.Postion).ToList(); //找到下一级
            if (dis.Count <= 0)
            {
                //Log.Debug("AddSonNode", "父节点" + p + "没下一级");
            }
            foreach (Model.Order_DishesType_T item in dis)
            {
                //Log.Debug("父节点" + p + ",下节点：" + item.ClassID);
                DishesTypeInfo dt = new DishesTypeInfo
                {
                    DishesTypeID = item.DishesTypeID,
                    DishesTypeName = item.DishesTypeName,
                    IntBZ = item.IntBZ,
                    Postion = item.Postion,
                    Stat = item.Stat,
                    Icon = item.Icon,
                    PantID = item.PantID,
                    Children = new List<DishesTypeInfo>()
                };
                odt.Children.Add(dt);
                AddSonNode(item.DishesTypeID, dt, roomDisList);
            }
        }
        #endregion

        /// <summary>
        /// 获取商品一级分类
        /// </summary>
        /// <param name="depid"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string Get_Order_DishesType_PC(string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion 
                List<Model.Order_DishesType_T> ls = new List<Model.Order_DishesType_T>();

                ls = base.GetListBy(u => u.Stat == 1 && (u.PantID == "" || u.PantID == null)).ToList();

                if (ls != null & ls.Count > 0)
                {
                    object tem = from m in ls select new { m.DishesTypeID, m.DishesTypeName, m.Icon, m.Postion, m.PantID };
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = tem;
                    result.total = ls.Count;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }

                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品分类信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        /// <summary>
        /// 获取商品二级分类
        /// </summary>
        /// <param name="depid"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string Get_Order_DishesType_PC_T(string DishesTypeID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion 
                List<Model.Order_DishesType_T> ls = new List<Model.Order_DishesType_T>();
                if (DishesTypeID != "" && DishesTypeID != null)
                {
                    ls = base.GetListBy(u => u.Stat == 1 && u.PantID == DishesTypeID).ToList();
                }
                if (ls != null && ls.Count > 0)
                {
                    object tem = from m in ls select new { m.DishesTypeID, m.DishesTypeName, m.Icon, m.Postion, m.PantID };
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = tem;
                    result.total = ls.Count;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }

                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品分类信息列表失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        /// <summary>
        /// 修改和明细的时候获取一级分类的名称
        /// </summary>
        /// <param name="DishesTypeID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string Get_Order_DishesTypeByDishesTypeID(string DishesTypeID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion                 
                List<Model.Order_DishesType_T> list = base.GetListBy(u => u.DishesTypeID == DishesTypeID);
                if (list != null && list.Count > 0)
                {
                    Model.Order_DishesType_T tmpup = list.First();
                    List<Model.Order_DishesType_T> ls = new List<Model.Order_DishesType_T>();
                    if (tmpup.PantID != "" && tmpup.PantID != null)
                    {
                        ls = base.GetListBy(u => u.Stat == 1 && u.DishesTypeID == tmpup.PantID).ToList();
                        if (ls != null && ls.Count > 0)
                        {
                            object tem = from m in ls select new { m.DishesTypeID, m.DishesTypeName };
                            result.BS = "1";
                            result.Msg = "查询成功";
                            result.rows = tem;
                            result.total = ls.Count;
                        }
                    }
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }

                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品一级分类名称失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        /// <summary>
        /// 修改和明细的时候获取二级分类的名称
        /// </summary>
        /// <param name="PantID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string Get_Order_DishesTypeByPantID(string PantID, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion 
                List<Model.Order_DishesType_T> ls = new List<Model.Order_DishesType_T>();
                if (PantID != "" && PantID != null)
                {
                    ls = base.GetListBy(u => u.Stat == 1 && u.DishesTypeID == PantID).ToList();
                    if (ls != null && ls.Count > 0)
                    {
                        object tem = from m in ls select new { m.DishesTypeID, m.DishesTypeName };
                        result.BS = "1";
                        result.Msg = "查询成功";
                        result.rows = tem;
                        result.total = ls.Count;
                    }
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }

                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品二级分类名称失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        /// <summary>
        /// 更新商品二级分类图片
        /// </summary>
        /// <param name="DishesTypeID"></param>
        /// <param name="DishesTypeURL"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string UpdateDishesTypePhoto(string DishesTypeID, string DishesTypeURL, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                int ret = -1;
                List<Model.Order_DishesType_T> list_tmp1 = base.GetListBy(u => u.DishesTypeID == DishesTypeID);
                if (list_tmp1 != null && list_tmp1.Count > 0)
                {
                    Model.Order_DishesType_T tmp = list_tmp1.First();
                    //tmp.Icon = Help.ALL_DataV.PhotoPath + "/DishesTypes/" + DishesTypeID + ".jpg";
                    tmp.Icon = DishesTypeURL;
                    ret = base.Modify(tmp);
                }
                if (ret > 0)
                {
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新商户图片失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商户图片失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "更新商户图片失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 获取商品一级分类名称
        /// </summary>
        /// <param name="dishestypename"></param>
        /// <returns></returns>
        public bool Get_Order_DishesTypeByName(string dishestypename)
        {
            try
            {
                List<Model.Order_DishesType_T> name = new List<Model.Order_DishesType_T>();

                name = base.GetListBy(u => u.DishesTypeName == dishestypename && (u.PantID == "" || u.PantID == null));

                if (name != null & name.Count > 0)
                {
                    object tem = from m in name select new { m.DishesTypeID, m.DishesTypeName };
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = tem;
                    result.total = name.Count;
                    return false;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                    return true;
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品一级分类名称失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return false;
            }
        }

        /// <summary>
        /// 获取商品二级级分类名称
        /// </summary>
        /// <param name="dishestypename"></param>
        /// <returns></returns>
        public bool Get_Order_DishesTypeByName2(string dishestypename)
        {
            try
            {
                List<Model.Order_DishesType_T> name = new List<Model.Order_DishesType_T>();

                name = base.GetListBy(u => u.DishesTypeName == dishestypename && (u.PantID != "" || u.PantID != null));

                if (name != null & name.Count > 0)
                {
                    object tem = from m in name select new { m.DishesTypeID, m.DishesTypeName };
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.rows = tem;
                    result.total = name.Count;
                    return false;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                    return true;
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 获取商品二级级分类名称失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return false;
            }
        }

        /// <summary>
        /// 更新一二级商品顺序
        /// </summary>
        /// <param name="dishestypeidup">上级商品ID</param>
        /// <param name="dishestypeiddown">下级商品ID</param>
        /// <param name="postionup">上级标志</param>
        /// <param name="postiondown">下级标志</param>
        /// <returns></returns>
        public string UpdateDishesTypePostion(string dishestypeidup, string dishestypeiddown, int postionup, int postiondown)
        {
            try
            {
                int ret = -1;
                int rets = -1;
                List<Model.Order_DishesType_T> list_up = base.GetListBy(u => u.DishesTypeID == dishestypeidup);
                List<Model.Order_DishesType_T> list_down = base.GetListBy(u => u.DishesTypeID == dishestypeiddown);
                if (list_up != null && list_up.Count > 0)
                {
                    Model.Order_DishesType_T tmpup = list_up.First();
                    tmpup.Postion = postionup;
                    ret = base.Modify(tmpup);
                    if (list_down != null && list_down.Count > 0)
                    {
                        Model.Order_DishesType_T tmpdown = list_down.First();
                        tmpdown.Postion = postiondown;
                        rets = base.Modify(tmpdown);
                    }
                }
                if (ret > 0 && rets > 0)
                {
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新一二级商品顺序失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新一二级商品顺序失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "更新一二级商品顺序失败";
            }

            return os.StringOfObject(result, 1);
        }

        /// <summary>
        /// 商品一二级排序
        /// </summary>
        /// <param name="OperID"></param>
        /// <param name="DishesTypeID"></param>
        /// <param name="Postion"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public string UpdateDishesTypeSort(int OperID, string DishesTypeID, int Postion, int RETID)
        {
            try
            {
                int ret = -1;
                List<Model.Order_DishesType_T> list = base.GetListBy(u => u.DishesTypeID == DishesTypeID);
                if (list != null && list.Count > 0)
                {
                    Model.Order_DishesType_T tmp = list.First();
                    tmp.Postion = Postion;
                    ret = base.Modify(tmp);
                }
                if (ret > 0)
                {
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新一二级商品顺序失败";
                }
            }
            catch (Exception ex)
            {
                result.BS = "-99";
                result.Msg = "更新一二级商品顺序失败" + ex.Message;
            }
            return os.StringOfObject(result, 1);
        }

    }
}
